package com.jbt.mds.obdsdk

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.jbt.mds.obdsdk.databinding.ActivityUpdateBinding
import com.jbt.mds.sdk.obd.ObdResource
import com.jbt.mds.sdk.obd.VciFirmwareResource
import com.jbt.mds.sdk.okhttp.model.Progress
import com.jbt.mds.sdk.okhttp.request.HttpRespondCode
import com.jbt.mds.sdk.vin.VinUtils

class UpdateActivity : AppCompatActivity() {

    private val TAG = "Obdljzt"
    lateinit var binding:ActivityUpdateBinding
    lateinit var mVciFirmwareResource: VciFirmwareResource
    lateinit var mObdResource:ObdResource
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityUpdateBinding.inflate(layoutInflater)
        mObdResource =  ObdResource.getInstance()
        mVciFirmwareResource = VciFirmwareResource.getInstance()
        setContentView(binding.root)
        binding.back.setOnClickListener {
            finish()
        }
        binding.btDownload.setOnClickListener {
            binding.loadingProgressBar.visibility = View.VISIBLE
            checkObdResourceIsHaveNew()
        }
        binding.btPauseDownload.setOnClickListener {
            mObdResource.pauseDownLoad()
            binding.tvStatus1.text = "下载暂停"
        }

        // 新增：VCL固件下载相关按钮
        binding.btnDownloadVcl.setOnClickListener {

            val firmwareVersion: String = mVciFirmwareResource.getLocalVersion()
            Log.i(
                TAG,
                "当前固件程序版本: $firmwareVersion"
            )
            // 启动下载更新之前， 必须先检测。
            checkVciFirmwareResourceIsHaveNew()

        }
        binding.btnMonitorVcl.setOnClickListener {
//            mObdResource.pauseVclDownLoad()
//            binding.tvStatus2.text = "下载暂停"
        }
        //下载当前诊断基础库版本
        binding.btnGetLocalVersion.setOnClickListener{
            checkOBDSDKVINUpdate()
        }
        //停止
        binding.btnMonitorLocal.setOnClickListener {

        }

    }

    fun checkObdResourceIsHaveNew(){
        Log.i(TAG, "当前诊断资源版本: ")
        mObdResource.isHaveNew(
            this
        ) { resultCode ->
            if (resultCode == HttpRespondCode.RESPOND_UPDATE_YES) {
                // 有更新， 下载更新
                Log.i(
                    TAG,
                    "obdUpdateResult: 有更新"
                )

                startObdResourceDownload()


            } else if (resultCode == HttpRespondCode.RESPOND_UPDATE_NO) {
                binding.tvStatus1.text = "无更新"
                binding.loadingProgressBar.visibility = View.GONE
                // 无更新
                Log.i(
                    TAG,
                    "obdUpdateResult: 无更新"
                )
            } else if (resultCode == HttpRespondCode.RESPOND_NETWORK_NONE) {
                // 网络不通
                binding.tvStatus1.text = "无网络连接"
                binding.loadingProgressBar.visibility = View.GONE
                Log.i(
                    TAG,
                    "obdUpdateResult: 无网络连接"
                )
            } else {
                // 其他错误
                Log.i(
                    TAG,
                    "obdUpdateResult: resultCode：$resultCode"
                )
            }
        }
    }
    // 下载诊断程序， 启动下载之前， 必须检测是否有更新
    fun startObdResourceDownload() {

        // 有更新， 下载更新
        mObdResource.startDownload { progress -> // 下载的进度

            Log.i(
                TAG,
                "onProgress: 状态：" + progress.status + ", 进度：" + progress.fraction
            )
            when (progress.status) {
                Progress.NONE -> {

                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: NONE")
                }
                Progress.WAITING -> {
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: WAITING")
                }
                Progress.LOADING -> {
                    binding.loadingProgressBar.visibility = View.VISIBLE
                    binding.tvStatus1.text = "下载中"
                    Log.d("ProgressStatus", "当前状态: LOADING")
                }
                Progress.PAUSE -> {
                    binding.tvStatus1.text = "下载暂停"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: PAUSE")
                }
                Progress.ERROR -> {
                    binding.tvStatus1.text = "下载错误"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: ERROR")
                }
                Progress.FINISH -> {
                    binding.tvStatus1.text = "下载完成"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: FINISH")
                }
                Progress.UNZIPPING -> {
                    Log.d("ProgressStatus", "当前状态: UNZIPPING")
                    binding.loadingProgressBar.visibility = View.GONE

                }
                Progress.NET_ERROR -> {
                    binding.tvStatus1.text = "网络错误"
                    Log.d("ProgressStatus", "当前状态: NET_ERROR")
                    binding.loadingProgressBar.visibility = View.GONE
                }
            }
        }
    }

//       固件程序版本 启动下载更新之前， 必须先检测。
    fun checkVciFirmwareResourceIsHaveNew() {
        mVciFirmwareResource.isHaveNew(
            this
        ) { resultCode ->
            if (resultCode == HttpRespondCode.RESPOND_UPDATE_YES) {
                // 有更新， 下载更新
                // 启动下载更新之前， 必须先检测。
                vciFrimwareDownload()
                Log.i(TAG, "VCI固件: 有更新")
            } else if (resultCode == HttpRespondCode.RESPOND_UPDATE_NO) {
                // 无更新
                Log.i(TAG, "VCI固件: 无更新")
                binding.tvStatus2.text = "无更新"
//                binding.loadingProgressBar.visibility = View.GONE
            } else if (resultCode == HttpRespondCode.RESPOND_NETWORK_NONE) {
                // 网络不通
                binding.tvStatus2.text = "无网络连接"
                Log.i(TAG, "VCI固件: 无网络连接")
            } else {
                // 其他错误
                Log.i(
                    TAG,
                    "VCI固件: resultCode：$resultCode"
                )
            }
        }
    }


    // VCI 固件下载更新， 启动之前必须先检测是否有更新。
    fun vciFrimwareDownload() {
        // 有更新， 下载更新
        mVciFirmwareResource.startDownload { progress -> // 下载的进度
            Log.i(
               TAG,
                "onProgress: 状态：" + progress.status + ", 进度：" + progress.fraction
            )

            when (progress.status) {
                Progress.NONE -> {

                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: NONE")
                }
                Progress.WAITING -> {
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: WAITING")
                }
                Progress.LOADING -> {
                    binding.loadingProgressBar.visibility = View.VISIBLE
                    binding.tvStatus1.text = "下载中"
                    Log.d("ProgressStatus", "当前状态: LOADING")
                }
                Progress.PAUSE -> {
                    binding.tvStatus1.text = "下载暂停"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: PAUSE")
                }
                Progress.ERROR -> {
                    binding.tvStatus1.text = "下载错误"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: ERROR")
                }
                Progress.FINISH -> {
                    binding.tvStatus2.text = "下载完成"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: FINISH")
                }
                Progress.UNZIPPING -> {
                    Log.d("ProgressStatus", "当前状态: UNZIPPING")
                    binding.loadingProgressBar.visibility = View.GONE

                }
                Progress.NET_ERROR -> {
                    binding.tvStatus1.text = "网络错误"
                    Log.d("ProgressStatus", "当前状态: NET_ERROR")
                    binding.loadingProgressBar.visibility = View.GONE
                }
            }

        }
    }

    // 检测诊断基础库是否有更新
    fun checkOBDSDKVINUpdate() {
        VinUtils.checkOBDSDKVINUpdate { resultCode ->
            if (resultCode == HttpRespondCode.RESPOND_UPDATE_YES) {
                // 有更新， 下载更新
                Log.i(TAG, "诊断基础库: 有更新")
                updateOBDSDKVIN()
            } else if (resultCode == HttpRespondCode.RESPOND_UPDATE_NO) {
                // 无更新
                binding.tvStatus3.text = "无更新"
                Log.i(TAG, "诊断基础库: 无更新")
            } else if (resultCode == HttpRespondCode.RESPOND_NETWORK_NONE) {
                // 网络不通
                binding.tvStatus3.text = "无网络连接"
                Log.i(TAG, "诊断基础库: 无网络连接")
            } else {
                // 其他错误
                Log.i(
                    TAG,
                    "诊断基础库: resultCode：$resultCode"
                )
            }
        }
    }


    // 下载更新检测诊断基础库 启动下载更新之前， 必须先检测是否有更新。
    fun updateOBDSDKVIN() {
        VinUtils.updateOBDSDKVIN { progress -> // 下载的进度
            Log.i(
               TAG,
                "onProgress: 状态：" + progress.status + ", 进度：" + progress.fraction
            )
            when (progress.status) {
                Progress.NONE -> {

                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: NONE")
                }
                Progress.WAITING -> {
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: WAITING")
                }
                Progress.LOADING -> {
                    binding.loadingProgressBar.visibility = View.VISIBLE
                    binding.tvStatus3.text = "下载中"
                    Log.d("ProgressStatus", "当前状态: LOADING")
                }
                Progress.PAUSE -> {
                    binding.tvStatus3.text = "下载暂停"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: PAUSE")
                }
                Progress.ERROR -> {
                    binding.tvStatus3.text = "下载错误"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: ERROR")
                }
                Progress.FINISH -> {
                    binding.tvStatus3.text = "下载完成"
                    binding.loadingProgressBar.visibility = View.GONE
                    Log.d("ProgressStatus", "当前状态: FINISH")
                }
                Progress.UNZIPPING -> {
                    Log.d("ProgressStatus", "当前状态: UNZIPPING")
                    binding.loadingProgressBar.visibility = View.GONE

                }
                Progress.NET_ERROR -> {
                    binding.tvStatus3.text = "网络错误"
                    Log.d("ProgressStatus", "当前状态: NET_ERROR")
                    binding.loadingProgressBar.visibility = View.GONE
                }
            }
        }
    }

}

